package com.kmproject.haifu.mvp.presenter;

import android.Manifest;
import android.app.Application;

import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.functions.Consumer;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
//import okhttp3.MediaType;
//import okhttp3.MultipartBody;
//import okhttp3.RequestBody;

import javax.inject.Inject;

import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.utils.PermissionUtil;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.hfinterface.AliYunUploadFileInterface;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.UserSetEditInfoContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.ui.widget.CircleImageView;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.List;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/24/2019 11:47
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class UserSetEditInfoPresenter extends BasePresenter<UserSetEditInfoContract.Model, UserSetEditInfoContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    String[] permission = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA,Manifest.permission.VIBRATE};
    ImageLoader glideImage;
    @Inject
    public UserSetEditInfoPresenter(UserSetEditInfoContract.Model model, UserSetEditInfoContract.View rootView) {
        super(model, rootView);
        if (glideImage == null) {
            glideImage = ArmsUtils.obtainAppComponentFromContext(AppManager.getAppManager().getCurrentActivity()).imageLoader();
        }
    }

    /**
     * 设置头像
     * @param header
     */
    public void updHeadPortrait(String header) {
        mModel.updHeadPortrait(header).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean))
                        {
                            mRootView.showMessage("头像设置成功");
                            mRootView.updHeadPortraitSuccessForServer(header);
                        }else {
                            mRootView.showMessage(resultBean.getMessage());
                        }

                    }
                });
    }

    /**
     * 上传图片
     * @param file
     */
    public void updateHeadImg(final CircleImageView circleImageView,  File file)
    {
        AppUtil.upfileToAli(mAppManager.getCurrentActivity(),Api.houseRent_server_packer, file.getPath(), new AliYunUploadFileInterface() {
            @Override
            public void AliYunUploadFileCallBackProgress(long currentSize, long totalSize) {

            }

            @Override
            public void AliYunUploadFileCallBackSuccess(PutObjectResult result,String serverPath) {
//               mRootView.updHeadPortraitSuccess(serverPath);
                updHeadPortrait(serverPath);
            }

            @Override
            public void AliYunUploadFileCallBackFailure(ServiceException serviceException) {

            }
        });
    }
    /**
     * 查询用户信息
     */
    public void qurUserInfo(){
        UserBean userBean=DataHelper.getDeviceData(AppManager.getAppManager().getCurrentActivity(),AppConstant.USER_INFO);
        if (userBean!=null)
        {
          mRootView.qurUserInfoSuccess(userBean);
        }
        mModel.qurUserInfo().compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<UserBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<UserBean> userBeanResultBean) {
                        if (!AppUtil.isSuccess(userBeanResultBean))
                        {
                            mRootView.showMessage(userBeanResultBean.getMessage());
                        }else {
                            DataHelper.saveDeviceData(mAppManager.getCurrentActivity(),AppConstant.USER_INFO,userBeanResultBean.getData());
                            mRootView.qurUserInfoSuccess(userBeanResultBean.getData());
                        }
                    }
                });
    }
    /**
     * 请求相机权限
     */
    public void requestPrisession(){
        //请求外部存储权限用于适配android6.0的权限管理机制
        PermissionUtil.requestPermission(new PermissionUtil.RequestPermission() {
            @Override
            public void onRequestPermissionSuccess() {
               mRootView.requestPerimissionSuccess();
            }

            @Override
            public void onRequestPermissionFailure(List<String> permissions) {
               mRootView.showMessage("请求相机权限失败");
            }

            @Override
            public void onRequestPermissionFailureWithAskNeverAgain(List<String> permissions) {
                mRootView.showMessage("请同意使用摄像头权限");
            }
        }, mRootView.getPermissions(), mErrorHandler, permission);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
